package com.cos.students.web.controller;

import com.alibaba.druid.util.StringUtils;
import com.cos.students.common.Const;
import com.cos.students.common.ResponseResult;
import com.cos.students.entity.RcStudent;
import com.cos.students.services.IRcSystemService;
import com.cos.students.util.TokenUtils;
import com.cos.students.vo.input.LoginInputVo;
import com.cos.students.vo.output.LoginInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author: COS
 * @time: 2022/10/25 9:47
 * @description:
 */
@RestController
public class SystemController {
    @Autowired
    private IRcSystemService systemService;

    @RequestMapping("/login")
    public ResponseResult doLogin(LoginInputVo inputVo, HttpSession session) {
        ResponseResult responseResult = systemService.doLogin(inputVo);
        System.out.println(session.getId());
        //保存数据到session
        LoginInfoVo  data = (LoginInfoVo) responseResult.getData();
        // 登出登录状态
        session.setAttribute("is_login",data);
        // 把登录的数据 resposneresult里面的data数据先进行jwt编码,重新设置会data
        // 把编译好的token令牌数据覆盖原有的明文数据
        String token = TokenUtils.getToken(data, Const.HAFT_HOUR);
        responseResult.setData(token);
        return responseResult;
    }

    @RequestMapping("/demo")
    public String demo(HttpSession session, HttpServletRequest request) {

//        System.out.println(is_login);
//        System.out.println(session.getId());
//        System.out.println("获取学员的登录数据的id:"+is_login.getUserId());
        return "xxx";
    }

}
